﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Brain
{
    public static class BrainModelReader
    {
        public static BrainModelContent Read(Stream stream)
        {
            StreamReader reader = new StreamReader(stream);

            bool readVersion = false;
            float version = 0;

            int meshesCount = -1;

            BrainModelContent model = new BrainModelContent();

            bool ExportNormals = false;
            bool ExportTangentSpaceInfo = false;
            bool ExportSkinning = false;
            bool ExportAnimation = false;

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                if (!readVersion)
                {
                    // this SHOULD be the first line
                    // of the system, we need to know
                    // what version the file was exporter
                    if (line.StartsWith("BMv"))
                    {
                        version = float.Parse(line.Split('v')[1], CultureInfo.InvariantCulture);
                        readVersion = true;
                    }
                }
                else
                {
                    if (meshesCount == -1)
                    {
                        // we need to find the meshes count
                        // this should be the 5th line
                        if (line.StartsWith("m:"))
                        {
                            meshesCount = int.Parse(line.Split(':')[1], CultureInfo.InvariantCulture);
                            model.Meshes = new BrainModelMeshContent[meshesCount];

                            for (int i = 0; i < meshesCount; i++)
                            {
                                BrainModelMeshContent mesh = new BrainModelMeshContent();
                                mesh.Vertices = new List<BrainModelVertexContent>();
                                mesh.Indices = new List<int>();
                                model.Meshes[i] = mesh;
                            }
                        }
                    }
                    else
                    {
                        if (line.StartsWith("v:"))
                        {
                            string[] vertex = line.Split(':');
                            float X = float.Parse(vertex[1], CultureInfo.InvariantCulture);
                            float Y = float.Parse(vertex[2], CultureInfo.InvariantCulture);
                            float Z = float.Parse(vertex[3], CultureInfo.InvariantCulture);
                            float TexX = float.Parse(vertex[4], CultureInfo.InvariantCulture);
                            float TexY = float.Parse(vertex[5], CultureInfo.InvariantCulture);
                            float NormalX = float.Parse(vertex[6], CultureInfo.InvariantCulture);
                            float NormalY = float.Parse(vertex[6], CultureInfo.InvariantCulture);
                            float NormalZ = float.Parse(vertex[6], CultureInfo.InvariantCulture);

                            Vector3 pos = new Vector3(X, Y, Z);
                            Vector2 tex = new Vector2(TexX, TexY);
                            Vector3 normal = new Vector3(NormalX, NormalY, NormalZ);

                            BrainModelVertexContent v = new BrainModelVertexContent();
                            v.Position = pos;
                            v.TexCoord = tex;
                            v.Normal = normal;

                            model.Meshes[0].Vertices.Add(v);

                            //VertexPositionNormal v = new VertexPositionNormal(pos, Vector3.Up);
                            //vertices[0].Add(v);
                        }
                        if (line.StartsWith("i:"))
                        {
                            string[] indice = line.Split(':');
                            int IX = int.Parse(indice[1], CultureInfo.InvariantCulture);
                            int IY = int.Parse(indice[2], CultureInfo.InvariantCulture);
                            int IZ = int.Parse(indice[3], CultureInfo.InvariantCulture);

                            model.Meshes[0].Indices.Add(IX);
                            model.Meshes[0].Indices.Add(IY);
                            model.Meshes[0].Indices.Add(IZ);
                        }
                    }
                }
            }

            reader.Dispose();

            return model;
        }
    }
}
